package com.itheima.health.security;

import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author by Dragon
 * @Classname EncodePassword
 * @Description TODO
 * @Date 2020/2/22 18:29
 * @Version 1.0
 */
public class EncodePassword implements PasswordEncoder {
    @Override
    public String encode(CharSequence rawPassword) {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder().encode(rawPassword);
    }
    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        if (encodedPassword.startsWith("{")) {
            String mode = encodedPassword.substring(1, encodedPassword.indexOf('}'));
            if (mode.equals("joker")) {
                return new JokerPasswordEncoder().matches(rawPassword,
                        encodedPassword.substring(encodedPassword.indexOf("}") + 1));
            }
        } else {
            encodedPassword = "{bcrypt}" + encodedPassword;
        }
        return PasswordEncoderFactories.createDelegatingPasswordEncoder().matches(rawPassword, encodedPassword);
    }


}

